<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-pragma">{-# LANGUAGE Trustworthy #-}</span><span>
</span><span id="line-2"></span><span class="hs-pragma">{-# LANGUAGE NoImplicitPrelude, MagicHash, UnboxedTuples #-}</span><span>
</span><span id="line-3"></span><span class="hs-pragma">{-# LANGUAGE BangPatterns #-}</span><span>
</span><span id="line-4"></span><span>
</span><span id="line-5"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-6"></span><span class="hs-comment">-- |</span><span>
</span><span id="line-7"></span><span class="hs-comment">-- Module      :  Data.IORef</span><span>
</span><span id="line-8"></span><span class="hs-comment">-- Copyright   :  (c) The University of Glasgow 2001</span><span>
</span><span id="line-9"></span><span class="hs-comment">-- License     :  BSD-style (see the file libraries/base/LICENSE)</span><span>
</span><span id="line-10"></span><span class="hs-comment">-- </span><span>
</span><span id="line-11"></span><span class="hs-comment">-- Maintainer  :  libraries@haskell.org</span><span>
</span><span id="line-12"></span><span class="hs-comment">-- Stability   :  experimental</span><span>
</span><span id="line-13"></span><span class="hs-comment">-- Portability :  portable</span><span>
</span><span id="line-14"></span><span class="hs-comment">--</span><span>
</span><span id="line-15"></span><span class="hs-comment">-- Mutable references in the IO monad.</span><span>
</span><span id="line-16"></span><span class="hs-comment">--</span><span>
</span><span id="line-17"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-18"></span><span>
</span><span id="line-19"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">Data.IORef</span><span>
</span><span id="line-20"></span><span>  </span><span class="hs-special">(</span><span> </span><span>
</span><span id="line-21"></span><span>        </span><span class="annot"><span class="hs-comment">-- * IORefs</span></span><span>
</span><span id="line-22"></span><span>        </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier">IORef</span></a></span><span class="hs-special">,</span><span>                </span><span class="hs-comment">-- abstract, instance of: Eq, Typeable</span><span>
</span><span id="line-23"></span><span>        </span><span class="annot"><a href="GHC.IORef.html#newIORef"><span class="hs-identifier">newIORef</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-24"></span><span>        </span><span class="annot"><a href="GHC.IORef.html#readIORef"><span class="hs-identifier">readIORef</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-25"></span><span>        </span><span class="annot"><a href="GHC.IORef.html#writeIORef"><span class="hs-identifier">writeIORef</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-26"></span><span>        </span><span class="annot"><a href="Data.IORef.html#modifyIORef"><span class="hs-identifier">modifyIORef</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-27"></span><span>        </span><span class="annot"><a href="Data.IORef.html#modifyIORef%27"><span class="hs-identifier">modifyIORef'</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-28"></span><span>        </span><span class="annot"><a href="Data.IORef.html#atomicModifyIORef"><span class="hs-identifier">atomicModifyIORef</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-29"></span><span>        </span><span class="annot"><a href="GHC.IORef.html#atomicModifyIORef%27"><span class="hs-identifier">atomicModifyIORef'</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-30"></span><span>        </span><span class="annot"><a href="Data.IORef.html#atomicWriteIORef"><span class="hs-identifier">atomicWriteIORef</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-31"></span><span>        </span><span class="annot"><a href="Data.IORef.html#mkWeakIORef"><span class="hs-identifier">mkWeakIORef</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-32"></span><span>        </span><span class="annot"><span class="hs-comment">-- ** Memory Model</span></span><span>
</span><span id="line-33"></span><span>
</span><span id="line-34"></span><span>        </span><span class="annot"><span class="hs-comment">-- $memmodel</span></span><span>
</span><span id="line-35"></span><span>
</span><span id="line-36"></span><span>        </span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-37"></span><span>
</span><span id="line-38"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Base.html"><span class="hs-identifier">GHC.Base</span></a></span><span>
</span><span id="line-39"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.STRef.html"><span class="hs-identifier">GHC.STRef</span></a></span><span>
</span><span id="line-40"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.IORef.html"><span class="hs-identifier">GHC.IORef</span></a></span><span>
</span><span id="line-41"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Weak.html"><span class="hs-identifier">GHC.Weak</span></a></span><span>
</span><span id="line-42"></span><span>
</span><span id="line-43"></span><span class="hs-comment">-- |Make a 'Weak' pointer to an 'IORef', using the second argument as a finalizer</span><span>
</span><span id="line-44"></span><span class="hs-comment">-- to run when 'IORef' is garbage-collected</span><span>
</span><span id="line-45"></span><span id="local-6989586621679550790"><span class="annot"><a href="Data.IORef.html#mkWeakIORef"><span class="hs-identifier hs-type">mkWeakIORef</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679550790"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Weak.html#Weak"><span class="hs-identifier hs-type">Weak</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679550790"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span></span><span>
</span><span id="line-46"></span><span id="mkWeakIORef"><span class="annot"><span class="annottext">mkWeakIORef :: forall a. IORef a -&gt; IO () -&gt; IO (Weak (IORef a))
</span><a href="Data.IORef.html#mkWeakIORef"><span class="hs-identifier hs-var hs-var">mkWeakIORef</span></a></span></span><span> </span><span id="local-6989586621679550745"><span class="annot"><span class="annottext">r :: IORef a
</span><a href="#local-6989586621679550745"><span class="hs-identifier hs-var">r</span></a></span></span><span class="hs-glyph">@</span><span class="hs-special">(</span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.STRef.html#STRef"><span class="hs-identifier hs-type">STRef</span></a></span><span> </span><span id="local-6989586621679550742"><span class="annot"><span class="annottext">MutVar# RealWorld a
</span><a href="#local-6989586621679550742"><span class="hs-identifier hs-var">r#</span></a></span></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span id="local-6989586621679550741"><span class="annot"><span class="annottext">State# RealWorld -&gt; (# State# RealWorld, () #)
</span><a href="#local-6989586621679550741"><span class="hs-identifier hs-var">finalizer</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, Weak (IORef a) #))
-&gt; IO (Weak (IORef a))
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, Weak (IORef a) #))
 -&gt; IO (Weak (IORef a)))
-&gt; (State# RealWorld -&gt; (# State# RealWorld, Weak (IORef a) #))
-&gt; IO (Weak (IORef a))
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679550740"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679550740"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-47"></span><span>    </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">MutVar# RealWorld a
-&gt; IORef a
-&gt; (State# RealWorld -&gt; (# State# RealWorld, () #))
-&gt; State# RealWorld
-&gt; (# State# RealWorld, Weak# (IORef a) #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#mkWeak%23"><span class="hs-identifier hs-var">mkWeak#</span></a></span><span> </span><span class="annot"><span class="annottext">MutVar# RealWorld a
</span><a href="#local-6989586621679550742"><span class="hs-identifier hs-var">r#</span></a></span><span> </span><span class="annot"><span class="annottext">IORef a
</span><a href="#local-6989586621679550745"><span class="hs-identifier hs-var">r</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld -&gt; (# State# RealWorld, () #)
</span><a href="#local-6989586621679550741"><span class="hs-identifier hs-var">finalizer</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679550740"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="hs-keyword">of</span><span> </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679550739"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679550739"><span class="hs-identifier hs-var">s1</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679550738"><span class="annot"><span class="annottext">Weak# (IORef a)
</span><a href="#local-6989586621679550738"><span class="hs-identifier hs-var">w</span></a></span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679550739"><span class="hs-identifier hs-var">s1</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Weak# (IORef a) -&gt; Weak (IORef a)
forall v. Weak# v -&gt; Weak v
</span><a href="GHC.Weak.html#Weak"><span class="hs-identifier hs-var">Weak</span></a></span><span> </span><span class="annot"><span class="annottext">Weak# (IORef a)
</span><a href="#local-6989586621679550738"><span class="hs-identifier hs-var">w</span></a></span><span> </span><span class="hs-special">#)</span><span>
</span><span id="line-48"></span><span>
</span><span id="line-49"></span><span class="hs-comment">-- |Mutate the contents of an 'IORef'.</span><span>
</span><span id="line-50"></span><span class="hs-comment">--</span><span>
</span><span id="line-51"></span><span class="hs-comment">-- Be warned that 'modifyIORef' does not apply the function strictly.  This</span><span>
</span><span id="line-52"></span><span class="hs-comment">-- means if the program calls 'modifyIORef' many times, but seldom uses the</span><span>
</span><span id="line-53"></span><span class="hs-comment">-- value, thunks will pile up in memory resulting in a space leak.  This is a</span><span>
</span><span id="line-54"></span><span class="hs-comment">-- common mistake made when using an IORef as a counter.  For example, the</span><span>
</span><span id="line-55"></span><span class="hs-comment">-- following will likely produce a stack overflow:</span><span>
</span><span id="line-56"></span><span class="hs-comment">--</span><span>
</span><span id="line-57"></span><span class="hs-comment">-- &gt;ref &lt;- newIORef 0</span><span>
</span><span id="line-58"></span><span class="hs-comment">-- &gt;replicateM_ 1000000 $ modifyIORef ref (+1)</span><span>
</span><span id="line-59"></span><span class="hs-comment">-- &gt;readIORef ref &gt;&gt;= print</span><span>
</span><span id="line-60"></span><span class="hs-comment">--</span><span>
</span><span id="line-61"></span><span class="hs-comment">-- To avoid this problem, use 'modifyIORef'' instead.</span><span>
</span><span id="line-62"></span><span id="local-6989586621679550776"><span class="annot"><a href="Data.IORef.html#modifyIORef"><span class="hs-identifier hs-type">modifyIORef</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679550776"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679550776"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679550776"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span><span>
</span><span id="line-63"></span><span id="modifyIORef"><span class="annot"><span class="annottext">modifyIORef :: forall a. IORef a -&gt; (a -&gt; a) -&gt; IO ()
</span><a href="Data.IORef.html#modifyIORef"><span class="hs-identifier hs-var hs-var">modifyIORef</span></a></span></span><span> </span><span id="local-6989586621679550734"><span class="annot"><span class="annottext">IORef a
</span><a href="#local-6989586621679550734"><span class="hs-identifier hs-var">ref</span></a></span></span><span> </span><span id="local-6989586621679550733"><span class="annot"><span class="annottext">a -&gt; a
</span><a href="#local-6989586621679550733"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">IORef a -&gt; IO a
forall a. IORef a -&gt; IO a
</span><a href="GHC.IORef.html#readIORef"><span class="hs-identifier hs-var">readIORef</span></a></span><span> </span><span class="annot"><span class="annottext">IORef a
</span><a href="#local-6989586621679550734"><span class="hs-identifier hs-var">ref</span></a></span><span> </span><span class="annot"><span class="annottext">IO a -&gt; (a -&gt; IO ()) -&gt; IO ()
forall (m :: * -&gt; *) a b. Monad m =&gt; m a -&gt; (a -&gt; m b) -&gt; m b
</span><a href="GHC.Base.html#%3E%3E%3D"><span class="hs-operator hs-var">&gt;&gt;=</span></a></span><span> </span><span class="annot"><span class="annottext">IORef a -&gt; a -&gt; IO ()
forall a. IORef a -&gt; a -&gt; IO ()
</span><a href="GHC.IORef.html#writeIORef"><span class="hs-identifier hs-var">writeIORef</span></a></span><span> </span><span class="annot"><span class="annottext">IORef a
</span><a href="#local-6989586621679550734"><span class="hs-identifier hs-var">ref</span></a></span><span> </span><span class="annot"><span class="annottext">(a -&gt; IO ()) -&gt; (a -&gt; a) -&gt; a -&gt; IO ()
forall b c a. (b -&gt; c) -&gt; (a -&gt; b) -&gt; a -&gt; c
</span><a href="GHC.Base.html#."><span class="hs-operator hs-var">.</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; a
</span><a href="#local-6989586621679550733"><span class="hs-identifier hs-var">f</span></a></span><span>
</span><span id="line-64"></span><span>
</span><span id="line-65"></span><span class="hs-comment">-- |Strict version of 'modifyIORef'</span><span>
</span><span id="line-66"></span><span class="hs-comment">--</span><span>
</span><span id="line-67"></span><span class="hs-comment">-- @since 4.6.0.0</span><span>
</span><span id="line-68"></span><span id="local-6989586621679550731"><span class="annot"><a href="Data.IORef.html#modifyIORef%27"><span class="hs-identifier hs-type">modifyIORef'</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679550731"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679550731"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679550731"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span><span>
</span><span id="line-69"></span><span id="modifyIORef%27"><span class="annot"><span class="annottext">modifyIORef' :: forall a. IORef a -&gt; (a -&gt; a) -&gt; IO ()
</span><a href="Data.IORef.html#modifyIORef%27"><span class="hs-identifier hs-var hs-var">modifyIORef'</span></a></span></span><span> </span><span id="local-6989586621679550729"><span class="annot"><span class="annottext">IORef a
</span><a href="#local-6989586621679550729"><span class="hs-identifier hs-var">ref</span></a></span></span><span> </span><span id="local-6989586621679550728"><span class="annot"><span class="annottext">a -&gt; a
</span><a href="#local-6989586621679550728"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-70"></span><span>    </span><span id="local-6989586621679550727"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679550727"><span class="hs-identifier hs-var">x</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IORef a -&gt; IO a
forall a. IORef a -&gt; IO a
</span><a href="GHC.IORef.html#readIORef"><span class="hs-identifier hs-var">readIORef</span></a></span><span> </span><span class="annot"><span class="annottext">IORef a
</span><a href="#local-6989586621679550729"><span class="hs-identifier hs-var">ref</span></a></span><span>
</span><span id="line-71"></span><span>    </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621679550726"><span class="annot"><span class="annottext">x' :: a
</span><a href="#local-6989586621679550726"><span class="hs-identifier hs-var hs-var">x'</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">a -&gt; a
</span><a href="#local-6989586621679550728"><span class="hs-identifier hs-var">f</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679550727"><span class="hs-identifier hs-var">x</span></a></span><span>
</span><span id="line-72"></span><span>    </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679550726"><span class="hs-identifier hs-var">x'</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; IO () -&gt; IO ()
</span><a href="../../ghc-prim/src/GHC.Prim.html#seq"><span class="hs-operator hs-var">`seq`</span></a></span><span> </span><span class="annot"><span class="annottext">IORef a -&gt; a -&gt; IO ()
forall a. IORef a -&gt; a -&gt; IO ()
</span><a href="GHC.IORef.html#writeIORef"><span class="hs-identifier hs-var">writeIORef</span></a></span><span> </span><span class="annot"><span class="annottext">IORef a
</span><a href="#local-6989586621679550729"><span class="hs-identifier hs-var">ref</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679550726"><span class="hs-identifier hs-var">x'</span></a></span><span>
</span><span id="line-73"></span><span>
</span><span id="line-74"></span><span class="hs-comment">-- |Atomically modifies the contents of an 'IORef'.</span><span>
</span><span id="line-75"></span><span class="hs-comment">--</span><span>
</span><span id="line-76"></span><span class="hs-comment">-- This function is useful for using 'IORef' in a safe way in a multithreaded</span><span>
</span><span id="line-77"></span><span class="hs-comment">-- program.  If you only have one 'IORef', then using 'atomicModifyIORef' to</span><span>
</span><span id="line-78"></span><span class="hs-comment">-- access and modify it will prevent race conditions.</span><span>
</span><span id="line-79"></span><span class="hs-comment">--</span><span>
</span><span id="line-80"></span><span class="hs-comment">-- Extending the atomicity to multiple 'IORef's is problematic, so it</span><span>
</span><span id="line-81"></span><span class="hs-comment">-- is recommended that if you need to do anything more complicated</span><span>
</span><span id="line-82"></span><span class="hs-comment">-- then using 'Control.Concurrent.MVar.MVar' instead is a good idea.</span><span>
</span><span id="line-83"></span><span class="hs-comment">--</span><span>
</span><span id="line-84"></span><span class="hs-comment">-- 'atomicModifyIORef' does not apply the function strictly.  This is important</span><span>
</span><span id="line-85"></span><span class="hs-comment">-- to know even if all you are doing is replacing the value.  For example, this</span><span>
</span><span id="line-86"></span><span class="hs-comment">-- will leak memory:</span><span>
</span><span id="line-87"></span><span class="hs-comment">--</span><span>
</span><span id="line-88"></span><span class="hs-comment">-- &gt;ref &lt;- newIORef '1'</span><span>
</span><span id="line-89"></span><span class="hs-comment">-- &gt;forever $ atomicModifyIORef ref (\_ -&gt; ('2', ()))</span><span>
</span><span id="line-90"></span><span class="hs-comment">--</span><span>
</span><span id="line-91"></span><span class="hs-comment">-- Use 'atomicModifyIORef'' or 'atomicWriteIORef' to avoid this problem.</span><span>
</span><span id="line-92"></span><span class="hs-comment">--</span><span>
</span><span id="line-93"></span><span id="local-6989586621679550763"><span id="local-6989586621679550764"><span class="annot"><a href="Data.IORef.html#atomicModifyIORef"><span class="hs-identifier hs-type">atomicModifyIORef</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679550764"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679550764"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679550764"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">,</span><span class="annot"><a href="#local-6989586621679550763"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679550763"><span class="hs-identifier hs-type">b</span></a></span></span></span><span>
</span><span id="line-94"></span><span id="atomicModifyIORef"><span class="annot"><span class="annottext">atomicModifyIORef :: forall a b. IORef a -&gt; (a -&gt; (a, b)) -&gt; IO b
</span><a href="Data.IORef.html#atomicModifyIORef"><span class="hs-identifier hs-var hs-var">atomicModifyIORef</span></a></span></span><span> </span><span id="local-6989586621679550722"><span class="annot"><span class="annottext">IORef a
</span><a href="#local-6989586621679550722"><span class="hs-identifier hs-var">ref</span></a></span></span><span> </span><span id="local-6989586621679550721"><span class="annot"><span class="annottext">a -&gt; (a, b)
</span><a href="#local-6989586621679550721"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-95"></span><span>  </span><span class="hs-special">(</span><span id="local-6989586621679550720"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679550720"><span class="hs-identifier hs-var">_old</span></a></span></span><span class="hs-special">,</span><span> </span><span class="hs-glyph">~</span><span class="hs-special">(</span><span id="local-6989586621679550719"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679550719"><span class="hs-identifier hs-var">_new</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679550718"><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679550718"><span class="hs-identifier hs-var">res</span></a></span></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IORef a -&gt; (a -&gt; (a, b)) -&gt; IO (a, (a, b))
forall a b. IORef a -&gt; (a -&gt; (a, b)) -&gt; IO (a, (a, b))
</span><a href="GHC.IORef.html#atomicModifyIORef2"><span class="hs-identifier hs-var">atomicModifyIORef2</span></a></span><span> </span><span class="annot"><span class="annottext">IORef a
</span><a href="#local-6989586621679550722"><span class="hs-identifier hs-var">ref</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; (a, b)
</span><a href="#local-6989586621679550721"><span class="hs-identifier hs-var">f</span></a></span><span>
</span><span id="line-96"></span><span>  </span><span class="annot"><span class="annottext">b -&gt; IO b
forall (f :: * -&gt; *) a. Applicative f =&gt; a -&gt; f a
</span><a href="GHC.Base.html#pure"><span class="hs-identifier hs-var">pure</span></a></span><span> </span><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679550718"><span class="hs-identifier hs-var">res</span></a></span><span>
</span><span id="line-97"></span><span>
</span><span id="line-98"></span><span class="hs-comment">-- | Variant of 'writeIORef' with the \&quot;barrier to reordering\&quot; property that</span><span>
</span><span id="line-99"></span><span class="hs-comment">-- 'atomicModifyIORef' has.</span><span>
</span><span id="line-100"></span><span class="hs-comment">--</span><span>
</span><span id="line-101"></span><span class="hs-comment">-- @since 4.6.0.0</span><span>
</span><span id="line-102"></span><span id="local-6989586621679550716"><span class="annot"><a href="Data.IORef.html#atomicWriteIORef"><span class="hs-identifier hs-type">atomicWriteIORef</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679550716"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679550716"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span><span>
</span><span id="line-103"></span><span id="atomicWriteIORef"><span class="annot"><span class="annottext">atomicWriteIORef :: forall a. IORef a -&gt; a -&gt; IO ()
</span><a href="Data.IORef.html#atomicWriteIORef"><span class="hs-identifier hs-var hs-var">atomicWriteIORef</span></a></span></span><span> </span><span id="local-6989586621679550713"><span class="annot"><span class="annottext">IORef a
</span><a href="#local-6989586621679550713"><span class="hs-identifier hs-var">ref</span></a></span></span><span> </span><span id="local-6989586621679550712"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679550712"><span class="hs-identifier hs-var">a</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-104"></span><span>  </span><span class="annot"><span class="annottext">a
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IORef a -&gt; a -&gt; IO a
forall a. IORef a -&gt; a -&gt; IO a
</span><a href="GHC.IORef.html#atomicSwapIORef"><span class="hs-identifier hs-var">atomicSwapIORef</span></a></span><span> </span><span class="annot"><span class="annottext">IORef a
</span><a href="#local-6989586621679550713"><span class="hs-identifier hs-var">ref</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679550712"><span class="hs-identifier hs-var">a</span></a></span><span>
</span><span id="line-105"></span><span>  </span><span class="annot"><span class="annottext">() -&gt; IO ()
forall (f :: * -&gt; *) a. Applicative f =&gt; a -&gt; f a
</span><a href="GHC.Base.html#pure"><span class="hs-identifier hs-var">pure</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-106"></span><span>
</span><span id="line-107"></span><span class="hs-comment">{- $memmodel

  In a concurrent program, 'IORef' operations may appear out-of-order
  to another thread, depending on the memory model of the underlying
  processor architecture.  For example, on x86, loads can move ahead
  of stores, so in the following example:

  &gt; import Data.IORef
  &gt; import Control.Monad (unless)
  &gt; import Control.Concurrent (forkIO, threadDelay)
  &gt;
  &gt; maybePrint :: IORef Bool -&gt; IORef Bool -&gt; IO ()
  &gt; maybePrint myRef yourRef = do
  &gt;   writeIORef myRef True
  &gt;   yourVal &lt;- readIORef yourRef
  &gt;   unless yourVal $ putStrLn &quot;critical section&quot;
  &gt;
  &gt; main :: IO ()
  &gt; main = do
  &gt;   r1 &lt;- newIORef False
  &gt;   r2 &lt;- newIORef False
  &gt;   forkIO $ maybePrint r1 r2
  &gt;   forkIO $ maybePrint r2 r1
  &gt;   threadDelay 1000000

  it is possible that the string @&quot;critical section&quot;@ is printed
  twice, even though there is no interleaving of the operations of the
  two threads that allows that outcome.  The memory model of x86
  allows 'readIORef' to happen before the earlier 'writeIORef'.

  The implementation is required to ensure that reordering of memory
  operations cannot cause type-correct code to go wrong.  In
  particular, when inspecting the value read from an 'IORef', the
  memory writes that created that value must have occurred from the
  point of view of the current thread.

  'atomicModifyIORef' acts as a barrier to reordering.  Multiple
  'atomicModifyIORef' operations occur in strict program order.  An
  'atomicModifyIORef' is never observed to take place ahead of any
  earlier (in program order) 'IORef' operations, or after any later
  'IORef' operations.

-}</span><span>
</span><span id="line-150"></span><span>
</span><span id="line-151"></span></pre></body></html>